//
//  UICollectionView+PlaceHolder.m
//  CHSimpleUI
//
//  Created by cyptt on 2022/8/15.
//

#import "UICollectionView+PlaceHolder.h"
#import <objc/runtime.h>
@interface UICollectionView()
/**占位图*/
@property (nonatomic, strong) UIView *placeHolderView;
@end

@implementation UICollectionView (PlaceHolder)


- (UIView *)placeHolderView {
    return objc_getAssociatedObject(self, @selector(placeHolderView));
}

- (void)setPlaceHolderView:(UIView *)placeHolderView {
    objc_setAssociatedObject(self, @selector(placeHolderView), placeHolderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(id)placeHolderDelegate{
    return objc_getAssociatedObject(self, @selector(placeHolderDelegate));
}

- (void)setPlaceHolderDelegate:(id<CHCollectionViewPlaceHolderDelegate>)placeHolderDelegate{
    objc_setAssociatedObject(self, @selector(placeHolderDelegate), placeHolderDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}




- (void)d_reloadData {
    [self reloadData];
    [self d_checkEmpty];
}

- (void)d_checkEmpty {
    BOOL isEmpty = YES;
    
    id<UICollectionViewDataSource> src = self.dataSource;
    NSInteger sections = 1;
    // 获取sections

    if ([src respondsToSelector: @selector(numberOfSectionsInCollectionView:)]) {
        sections = [src numberOfSectionsInCollectionView:self];
    }
    // 获取rows
    for (int i = 0; i<sections; ++i) {
       
        NSInteger rows =  [src collectionView:self numberOfItemsInSection:i];
        // 如果rows不为零
        if (rows > 0) {
            isEmpty = NO;
            break;
        }
        
    }
    
    // 如果rows为空
    if (isEmpty) {
    
        // 模式ScrollView可以滚动
        BOOL scrollEnabled = YES;
        if (self.placeHolderDelegate && [self.placeHolderDelegate respondsToSelector:@selector(enableScrollWhenPlaceHolderViewShowing)]) {
          
            scrollEnabled = [self.placeHolderDelegate enableScrollWhenPlaceHolderViewShowing];
        }
        self.scrollEnabled = scrollEnabled;
     
        
        // 移除placeHolderView
        [self.placeHolderView removeFromSuperview];
        // 获取placeHolderView
        if ([self respondsToSelector:@selector(makePlaceHolderView)]) {
            self.placeHolderView = [self performSelector:@selector(makePlaceHolderView)];
        }
        

        if ([self.placeHolderDelegate respondsToSelector:@selector(makePlaceHolderView)]) {
            self.placeHolderView = [self.placeHolderDelegate makePlaceHolderView];
        }
    
        //headview/footer 高度
        UICollectionViewFlowLayout * layout = (UICollectionViewFlowLayout*)self.collectionViewLayout;
    
        CGFloat HEADER_H = layout.headerReferenceSize.height;
        CGFloat FOOTER_H = layout.footerReferenceSize.height;
        
        self.placeHolderView.frame = CGRectMake(0,HEADER_H, self.frame.size.width, self.frame.size.height - HEADER_H - FOOTER_H );
      
        [self addSubview:self.placeHolderView];
    }
    else {
        // rows不为空 移除placeHolderView
        [self.placeHolderView removeFromSuperview];
        self.placeHolderView = nil;
        // 设置TableView 可滚动
        self.scrollEnabled = YES;
    }
}
@end

